import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/model/love_gallery_model.dart' as LGModel;
import 'package:love_app/providers/love_grallery.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/service/love_gallery_api.dart';
import 'package:love_app/style/space_header.dart.dart';
import 'package:provider/provider.dart';

import 'love_grallery_ui/every_item.dart';

// 获取恋爱相册
class LoveGallery extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ShareProvider _shareProvider = Provider.of<ShareProvider>(context, listen: false);
    return Scaffold(
      appBar: Su().appBar(
        "相册",
        colors: _shareProvider.themeColor,
        // actionText: "上传图片",
        actionTextStyle: Su().textStyle(Colors.white, 26),
      ),
      body: _LoveGallery(
          token: _shareProvider.token,
          textColor: _shareProvider.textColor,
        ),
    );
  }
}

// 获取恋爱相册页面内容
class _LoveGallery extends StatefulWidget {
  final String token;
  final Color textColor;

  const _LoveGallery({Key key, this.token, this.textColor}) : super(key: key);

  @override
  __LoveGalleryState createState() => __LoveGalleryState();
}

class __LoveGalleryState extends State<_LoveGallery>{
  // LGModel.LoveGalleryModel loveGalleryModel;
  LoveGralleryProvider _loveGralleryProvider;
  ScrollController _scrollController;
  ShareProvider _shareProvider;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _loveGralleryProvider = Provider.of<LoveGralleryProvider>(context, listen: false);
    _shareProvider = Provider.of<ShareProvider>(context, listen: false);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _scrollController = ScrollController(initialScrollOffset: _loveGralleryProvider.offset);
    _scrollController.addListener((){
      _loveGralleryProvider.offset = _scrollController.offset;
    });
    // return FutureBuilder(
    //   future: _getLoveGallery(page: _loveGralleryProvider.page),
    //   builder: (BuildContext context, AsyncSnapshot snapshot) {
    //     if (snapshot.connectionState == ConnectionState.done) {
    //       return _pageCentent(_scrollController);
    //     }
    //     return SuDialog.futureBuildLoding(textColor: _shareProvider.textColor);
    //   },
    // );
    return _pageCentent(_scrollController);
  }

  // 获取恋爱相册数据
  Future<void> _getLoveGallery({@required int page}) async {
    LGModel.LoveGalleryModel loveGalleryModel =
        await LoveGalleryApi().loveGallery(widget.token, page: page);
    if(loveGalleryModel.data.isEmpty == false){
      List<LGModel.Data> itemData = loveGalleryModel.data;
      _loveGralleryProvider.addGraleryItemData(itemData);
    }else{
      _loveGralleryProvider.isEndPage = true;
    }
  }

  // 可滚动的容器
  Widget _pageCentent(ScrollController scrollController) {
    List<LGModel.Data> itemList = _loveGralleryProvider.itemData;
    return EasyRefresh.custom(
      bottomBouncing: true,
      scrollController: scrollController,
      firstRefresh: true,
      header: SpaceHeader(),
      onRefresh: () async {
        // 触发下拉加载
        _loveGralleryProvider.clean();
        await _getLoveGallery(page: _loveGralleryProvider.page);
        setState(() {});
      },
      onLoad: () async {
        // 触发上拉加载
       if(_loveGralleryProvider.isEndPage == false){
         await _getLoveGallery(page: _loveGralleryProvider.page);
         setState(() {});
       }
      },
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) {
              return EveryItem(itemData: itemList[index], textColor: widget.textColor);
            },
            childCount: itemList.length,
          ),
        ),
      ],
    );
  }
}
